/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package movierental.ui.servlet;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import movierental.entity.Movie;
import movierental.service.AppService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author abet
 */
public class DaftarMovie extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.setContentType("text/html");
       
       
                 ApplicationContext ctx = WebApplicationContextUtils
		.getWebApplicationContext(req.getSession().getServletContext());
		
		AppService service = (AppService) ctx.getBean("appService");
		
		List<Movie> movi = service.findAllMovie();
		
		resp.setContentType("text/html");
		
		String output = "<html>";
		output += "<head>";
		output += "<title>Daftar Movie</title>";
		output += "</head>";
		output += "<body>";
		output += "<h1>Daftar Movie</h1>";
		
		output += "<table>";
		output += "<tr>";
		output += "<td>Kode</td>";
		output += "<td>Nama</td>";
                output += "<td>Judul</td>";
		output += "<td>Harga Sewa</td>";
		output += "<td>&nbsp;</td>";
		output += "</tr>";

		for (Movie m : movi) {
			output += "<tr>";
			output += "<td>"+m.getKodeMovie()+"</td>";
			output += "<td>"+m.getJudulMovie()+"</td>";
                        output += "<td>"+m.getFormatMovie()+"</td>";
                        output += "<td>"+m.getHargaMovie()+"</td>";
			output += "<td><a href='edit?id="+m.getKodeMovie()+"'>edit</a>";
			output += " | <a href='delete?id="+m.getKodeMovie()+"'>delete</a></td>";
			output += "</tr>";
		}
		
		output += "</table>";
		
		output += "</body>";
		output += "</html>";
		
		resp.getWriter().print(output);
		resp.getWriter().close();
      
    }
    
}
